<?php
// Configuration
require_once('config.php');

//print_r($_SERVER);
//echo $_SERVER['HTTP_REFERER'];
//if(!preg_match('@/inductions/@', $_SERVER['HTTP_REFERER']) && !isset($_GET['stop_rd'])) {
//	header('location:index.php?stop_rd=1');
//}

// Registry
$registry = new Registry();

// Loader
$loader = new Loader($registry);
$registry->set('load', $loader);

// Config
$config = new OpenConfig();
$registry->set('config', $config);

// Database
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db);
		
// Url
$url = new Url(HTTP_SERVER, HTTPS_SERVER);	
$registry->set('url', $url);
		
function error_handler($errno, $errstr, $errfile, $errline) {
	global $log, $config;
	
	switch ($errno) {
		case E_NOTICE:
		case E_USER_NOTICE:
			$error = 'Notice';
			break;
		case E_WARNING:
		case E_USER_WARNING:
			$error = 'Warning';
			break;
		case E_ERROR:
		case E_USER_ERROR:
			$error = 'Fatal Error';
			break;
		default:
			$error = 'Unknown';
			break;
	}
		
	if ($config->get('config_error_display')) {
		echo '<b>' . $error . '</b>: ' . $errstr . ' in <b>' . $errfile . '</b> on line <b>' . $errline . '</b>';
	}
	
	if ($config->get('config_error_log')) {
		$log->write('PHP ' . $error . ':  ' . $errstr . ' in ' . $errfile . ' on line ' . $errline);
	}

	return true;
}

// Error Handler
set_error_handler('error_handler');
		
// Request
$request = new Request();
$registry->set('request', $request);

// Response
$response = new Response();
$response->addHeader('Content-Type: text/html; charset=utf-8');
$registry->set('response', $response); 

// Session
$session = new Session();
$registry->set('session', $session); 

// Language	
$language = new Language('english');
$language->load('english');	
$registry->set('language', $language); 		

// Document
$document = new Document();
$registry->set('document', $document); 		
		
// Front Controller
$controller = new Front($registry);

// User
$registry->set('user', new User($registry));

// Router
if (isset($request->get['route']) && $request->get['route'] != WEB_HOME) {
	$action = new Action($request->get['route']);
} else {
	$action = new Action(WEB_HOME);
}

// Dispatch
$controller->dispatch($action, new Action('error/not_found'));

// Output
$response->output();
?>
